package com.gitee.xhs.sampleRecod.transactionRecord;

import com.gitee.xhs.sampleRecod.transactionRecord.jmockTool.TransactionLoopOneToFiveMockerInterceptor;
import com.gitee.xhs.sourceIterator.AbstractSourceIterator;
import com.github.jsonzou.jmockdata.JMockData;

import java.sql.Timestamp;

/**
 * @author BigHead
 * @date 2022/6/28 16:54
 * @description Transaction生成工具
 */
public class TransactionIterator extends AbstractTransactionIterator {


    public static TransactionIterator defaultFiveLoopBounded() {
        return new TransactionIterator(true, new TransactionLoopOneToFiveMockerInterceptor(), null, null, null, null);
    }

    public static TransactionIterator defaultFiveLoopUnbounded() {
        return new TransactionIterator(false, new TransactionLoopOneToFiveMockerInterceptor(), null, null, null, null);
    }

    public TransactionIterator(boolean bounded, AbstractTransactionMockerInterceptor abstractTransactionMockerInterceptor, Integer maxBounded, Long initialTimeStamp, Long intervalTimeStamp, Boolean useOutTimeStamp) {
        super(bounded, abstractTransactionMockerInterceptor, maxBounded, initialTimeStamp, intervalTimeStamp, useOutTimeStamp);
    }


    @Override
    public boolean hasNext() {
        if (index < maxBounded) {
            return true;
        } else {
            return !bounded;
        }
    }

    @Override
    public Transaction next() {
        this.abstractTransactionMockerInterceptor.getDataNum(index++);
        Transaction transaction = JMockData.mock(Transaction.class, this.abstractTransactionMockerInterceptor.getMockConfig());
        transaction.setDataNum(index);
        if (this.useOutTimeStamp) {
            transaction.setTimestamp(iteratorTime(null));
        } else {
            transaction.setTimestamp(timestamp);
        }
        timestamp += intervalTimeStamp;
        return transaction;
    }

}
